import pygame
import random

# 屏幕的宽高
screen_size = (600, 600)
# 每秒最多多少帧
zhen = 50
# 球的坐标
points = [[random.randint(50,550), 30]]
rect = [300, 500, 100, 40]
score = 0
life = 3
n = 0


def main():
    """主函数"""
    global n
    pygame.init()
    screen = pygame.display.set_mode(screen_size)
    is_loop = True
    clock = pygame.time.Clock()
    # 设置字体以及字体大小
    font = pygame.font.Font(None, 30)
    while is_loop:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                is_loop = False
            #     当事件是键盘落下时
            if event.type == pygame.KEYDOWN:
                # 判断key是否是left键
                if event.key == pygame.K_LEFT:
                    rect[0] -= 20
                    # 判断当前位置是否是在界面内，不是的话回到界面里

                    if rect[0] < 0:
                        rect[0] = 0
                elif event.key == pygame.K_RIGHT:
                    rect[0] += 20
                    # 判断当前位置是否是在界面内，不是的话回到界面里
                    if rect[0] > 600 - rect[2]:
                        rect[0] = 600 - rect[2]
        screen.fill((0, 0, 0))
        for point in points:
            pygame.draw.circle(screen, (255, 0, 0), point, 20)
            point[1] += 2

        # 绘制矩形区域
        pygame.draw.rect(screen, (0, 255, 0), rect)
        # 渲染的文字
        score_text = font.render(f"Score:{score}", True, (255, 0, 0))
        # 将渲染的文字放到屏幕的对应位置上
        score_life = font.render(f"Life:{life}", True, (255, 0, 0))
        screen.blit(score_text, [10, 10])
        screen.blit(score_life, [540, 10])
        if n > 50:
            points.append([random.randint(50, 550), 30])
            n=0
        n+=1
        clock.tick(zhen)
        pygame.display.update()
    pygame.quit()


main()

